package com.neusoft.bizcore.auth.converter;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.neusoft.bizcore.auth.dto.DingConfigDTO;
import com.neusoft.bizcore.auth.model.DingConfig;
import com.neusoft.bizcore.auth.repository.DingConfigRepository;
import com.neusoft.bizcore.web.dto.AbstractConverter;

@Component
public class DingConfigConverter extends AbstractConverter<DingConfig, DingConfigDTO> {

    @Autowired
    private DingConfigRepository dingConfigRepository;

    @Override
    public DingConfig toModel(final DingConfigDTO dto) {
        DingConfig model;
        if (dto.isNew()) {
            model = new DingConfig();
        } else {
            model = this.dingConfigRepository.getOne(dto.getId());
        }
        model.setAgentId(dto.getAgentId());
        model.setCorpId(dto.getCorpId());
        model.setAppKey(dto.getAppKey());
        model.setAppSecret(dto.getAppSecret());
        model.setName(dto.getName());
        return model;
    }

    @Override
    public DingConfigDTO toDTO(final DingConfig model, final boolean forListView) {
        final DingConfigDTO dto = new DingConfigDTO();
        dto.setId(model.getId());
        dto.setAgentId(model.getAgentId());
        dto.setCorpId(model.getCorpId());
        dto.setAppKey(model.getAppKey());
        dto.setAppSecret(model.getAppSecret());
        dto.setName(model.getName());
        return dto;
    }

}
